package com.qidianit.mes.production.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.pojo.Material;
import com.qidianit.mes.pojo.Process;
import com.qidianit.mes.pojo.WorkmanshipComposition;
import com.qidianit.mes.pojo.WorkmanshipMaterial;
import com.qidianit.mes.production.mapper.WorkmanshipMaterialMapper;
import com.qidianit.mes.production.service.WorkmanshipMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author djl
 * @Time 2024/10/22 20:11
 */
@Service
public class WorkmanshipMaterialServiceImpl implements WorkmanshipMaterialService {
    @Autowired
    private WorkmanshipMaterialMapper workmanshipMaterialMapper;

    @Override
    public MESResult list(WorkmanshipMaterial workmanshipMaterial, int page, int limit) {
        // 开启分页  参数1： 页码  参数2： 每页大小
        PageHelper.startPage(page, limit);
        // 查询全部
        List<WorkmanshipMaterial> workmanshipMaterials = workmanshipMaterialMapper.list(workmanshipMaterial);
        PageInfo<WorkmanshipMaterial> pageInfo = new PageInfo<>(workmanshipMaterials);
        // 将pageInfo对象设置到qdResult中
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult add(WorkmanshipMaterial workmanshipMaterial) {
        if(workmanshipMaterialMapper.queryByMaterialId(workmanshipMaterial.getMaterialId()) == null){
            workmanshipMaterialMapper.add(workmanshipMaterial);
            return MESResult.success(Constant.RESPONSE_STATUS_OK);
        }
        return MESResult.error(Constant.RESPONSE_STATUS_REPEAT);
    }

    @Override
    public MESResult edit(WorkmanshipMaterial workmanshipMaterial) {
        if(workmanshipMaterialMapper.queryByMaterialId(workmanshipMaterial.getMaterialId()) == null){
            workmanshipMaterialMapper.edit(workmanshipMaterial);
            return MESResult.success(Constant.RESPONSE_STATUS_OK);
        }else if(workmanshipMaterialMapper.queryByMaterialId(workmanshipMaterial.getMaterialId()).getWorkmanshipMaterialId() == workmanshipMaterial.getWorkmanshipMaterialId()){
            workmanshipMaterialMapper.edit(workmanshipMaterial);
            return MESResult.success(Constant.RESPONSE_STATUS_OK);
        }
        return null;
    }

    @Override
    public MESResult listById(WorkmanshipMaterial workmanshipMaterial) {
        return MESResult.success(workmanshipMaterialMapper.listByIdAndCode(workmanshipMaterial));
    }
}
